package cf.huzpsb.bcgod;

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

import java.util.Random;

public class CodeCMD extends Command {
    public CodeCMD(String name, String permission, String... aliases) {
        super(name, permission, aliases);
    }

    public void execute(CommandSender sender, String[] args) {
        if (!(sender instanceof ProxiedPlayer)) {
            if (args.length != 2) {
                sender.sendMessage("§c参数错误:/bcode <个数> <面值>");
                return;
            }
            int num;
            int val;
            try {
                num = Integer.parseInt(args[0]);
                val = Integer.parseInt(args[1]);
            } catch (Exception e) {
                sender.sendMessage("§c参数错误:你输入的不是整数");
                return;
            }
            if (num > 101) {
                sender.sendMessage("§c参数错误:你输入的个数太大了!最多为100!");
                return;
            }
            sender.sendMessage("------------------Bcgod随机数生成器------------------");
            sender.sendMessage("面值: " + val);
            for (int i = 0; i < num; i++) {
                String str = ranstr();
                if (Main.codecfg.contains(str)) {
                    sender.sendMessage("§c内部错误:你买彩票中奖了!随机数生成出现了重复! :D");
                    return;
                }
                Main.codecfg.set(str, val);
                sender.sendMessage(str);
            }
            sender.sendMessage("------------------Bcgod随机数生成器------------------");
            sender.sendMessage("§a生成成功,请及时复制!");
        } else if (args.length != 1) {
            sender.sendMessage("§c参数错误");
        } else if (((ProxiedPlayer) sender).getServer().getInfo().getName().equalsIgnoreCase(Main.login)) {
            sender.sendMessage("§c登录区域禁止使用!");
        } else {
            if (!Main.codecfg.contains(args[0])) {
                sender.sendMessage("§c充值卡输入错误呢.请注意区分大小写,并且我们推荐复制粘贴!");
                return;
            }
            int nh = 0;
            if (Main.horncfg.contains(sender.getName())) {
                nh = Main.horncfg.getInt(sender.getName());
            }
            nh += Main.codecfg.getInt(args[0]);
            sender.sendMessage("§a充值 " + Main.codecfg.getInt(args[0]) + " 成功,您现在的欢乐券个数: " + nh);
            Main.horncfg.set(sender.getName(), nh);
            Main.codecfg.set(args[0], null);
        }
    }

    private String ranstr() {
        String sb = "";
        Random rdm = new Random();
        for (int i = 0; i < 10; i++) {
            sb = sb + Main.codes.charAt(rdm.nextInt(Main.codes.length()));
        }
        return sb;
    }
}
